using System;
using System.Web;
using System.Web.UI;
using THANHTUNGO.Portal.Components.Settings;
using THANHTUNGO.Portal.Components.Security;

namespace THANHTUNGO.Portal.Components.UI
{
    public class YAFUrlBuilder
    {
        public static string GetLink(int moduleID, Enum page)
        {
            int tabID = 0;
            int tabIndex = 0;
            if (HttpContext.Current.Items["PortalSettings"] != null)
            {
                PortalSettings settings = (PortalSettings) HttpContext.Current.Items["PortalSettings"];
                tabID = settings.ActiveTab.ID;
                tabIndex = settings.ActiveTab.Index;
            }

            return string.Format("~/Default.aspx?tabindex={0}&tabid={1}&Page{2}={3}", tabIndex, tabID, moduleID, page);
        }

        public static string GetLink(bool forHtmlControl, int moduleID, Enum page)
        {
            int tabID = 0;
            int tabIndex = 0;
            if (HttpContext.Current.Items["PortalSettings"] != null)
            {
                PortalSettings settings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
                tabID = settings.ActiveTab.ID;
                tabIndex = settings.ActiveTab.Index;
            }

            return string.Format("Default.aspx?tabindex={0}&tabid={1}&Page{2}={3}", tabIndex, tabID, moduleID, page);
        }

        public static string GetLink(bool forHtmlControl, int moduleID, Enum page, string format, params object[] args)
        {
            int tabID = 0;
            int tabIndex = 0;
            if (HttpContext.Current.Items["PortalSettings"] != null)
            {
                PortalSettings settings = (PortalSettings)HttpContext.Current.Items["PortalSettings"];
                tabID = settings.ActiveTab.ID;
                tabIndex = settings.ActiveTab.Index;
            }

            return string.Format("Default.aspx?tabindex={0}&tabid={1}&Page{2}={3}&{4}", tabIndex, tabID, moduleID, page, string.Format(format, args));
        }

        public static string GetLink(int moduleID, Enum page, string format, params object[] args)
        {
            int tabID = 0;
            int tabIndex = 0;
            if (HttpContext.Current.Items["PortalSettings"] != null)
            {
                PortalSettings settings = (PortalSettings)HttpContext.Current.Items["PortalSettings"]; 
                tabID = settings.ActiveTab.ID;
                tabIndex = settings.ActiveTab.Index;
            }

            return string.Format("~/Default.aspx?tabindex={0}&tabid={1}&Page{2}={3}&{4}", tabIndex, tabID, moduleID, page, string.Format(format, args));
        }

        public static void Redirect(int moduleID, Enum page)
        {
            HttpContext.Current.Response.Redirect(GetLink(moduleID, page));
        }

        public static void Redirect(int moduleID, Enum page, string format, params object[] args)
        {
            HttpContext.Current.Response.Redirect(GetLink(moduleID, page, format, args));
        }
    }

    
    public class YAFAddEditPage : YAFPage
    {
        protected override void OnInit(EventArgs e)
        {
            if ((PortalSecurity.HasAddPermission(this.ModuleID) == false) && (PortalSecurity.HasEditPermission(this.ModuleID) == false))
            {
                PortalSecurity.AccessEditDenied();
            }
            base.OnInit(e);
        }
    }

    public class YAFPage : UserControl
    {
        private PortalModuleControl m_Container = null;
        public PortalModuleControl ParentControl
        {
            get
            {
                if (m_Container != null)
                    return m_Container;

                Control ctl = Parent;
                while (ctl.GetType() != typeof (PortalModuleControl))
                    ctl = ctl.Parent;

                m_Container = (PortalModuleControl) ctl;
                return m_Container;
            }
            set { m_Container = value; }
        }

        public bool IsViewable
        {
            get { return ParentControl.IsViewable; }
        }

        public bool IsAddable
        {
            get { return ParentControl.IsAddable; }
        }

        public bool IsEditable
        {
            get { return ParentControl.IsEditable; }
        }

        public bool IsDeleteable
        {
            get { return ParentControl.IsDeleteable; }
        }

        public bool IsManagerable
        {
            get { return ParentControl.IsManagerable; }
        }

        public int ModuleID
        {
            get { return ParentControl.ModuleID; }
        }

        public int TabID
        {
            get { return ParentControl.TabID; }
        }

        public int TabIndex
        {
            get { return ParentControl.TabIndex; }
        }

        private int _itemID = 0;
        public int ItemID
        {
            get
            {
                if (_itemID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["ItemID"] != null)
                        _itemID = Int32.Parse(Request.Params["ItemID"]);
                }
                return _itemID;
            }
            set { _itemID = value; }
        }
    }
}